package com.rrd.dw.mr.gzip.kafka;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.baihang.BaihangFRAI001ARecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 百行-特别关注名单2.0
 *
 * @author guoqiang
 */
public class BaihangFRAI001AParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 百行-特别关注名单2.0
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        BaihangFRAI001ARecord obj = (BaihangFRAI001ARecord) model;
        StringBuilder sb = getBaseInfo(obj, successful);
        String data = obj.getData();
        JSONObject dataJson = JSON.parseObject(data);
        JSONObject head = dataJson.getJSONObject("head");
        sb.append(formatValue(head.getString("requestRefId"))).append(CRT001);
        sb.append(formatValue(head.getString("responseRefId"))).append(CRT001);
        sb.append(formatValue(head.getString("result"))).append(CRT001);
        sb.append(formatValue(head.getString("responseCode"))).append(CRT001);
        sb.append(formatValue(head.getString("responseMsg"))).append(CRT001);

        JSONObject response = dataJson.getJSONObject("response");
        sb.append(formatValue(response.getString("p2pEscapeDebtStatus"))).append(CRT001);
        sb.append(formatValue(response.getString("supremeCourtExecutedStatus"))).append(CRT001);
        sb.append(formatValue(response.getString("maxOverdueStatus"))).append(CRT001);
        sb.append(formatValue(response.getString("overdue60D_36M"))).append(CRT001);
        sb.append(formatValue(response.getString("overdue30D_24M"))).append(CRT001);
        sb.append(formatValue(response.getString("overdue15D_12M"))).append(CRT001);
        sb.append(formatValue(response.getString("firstOverdue7D_24M"))).append(CRT001);
        sb.append(formatValue(response.getString("largeAmtOverdue30D_36M"))).append(CRT001);
        sb.append(formatValue(response.getString("multiPartyLoanStatus"))).append(CRT001);
        sb.append(formatValue(response.getString("multiPartyLoan3_90D"))).append(CRT001);
        sb.append(formatValue(response.getString("multiQuasiFin3_180D"))).append(CRT001);
        sb.append(formatValue(response.getString("multiSubsidy2_180D"))).append(CRT001);
        sb.append(formatValue(response.getString("multiQuery8_180D"))).append(CRT001);
        sb.append(formatValue(response.getString("multiQuery5_30D"))).append(CRT001);
        sb.append(formatValue(response.getString("multiQuery6_90D"))).append(CRT001);
        sb.append(formatValue(response.getString("multiQuery3_3D"))).append(CRT001);
        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_baihang_fraia_inc";
    }

    @Override
    public String getType() {
        return RecordType.BAIHANG_FRAI001A_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "百行-特别关注名单2.0";
    }
}
